package edu.com.wjt219971332.dao;

import edu.com.wjt219971332.entity.User;
import org.apache.ibatis.annotations.*;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface UserDao {

    @Select("SELECT * FROM user WHERE userid = #{userid}")
    User queryById(Integer userid);

    @Select("SELECT * FROM user LIMIT #{pageable.offset}, #{pageable.pageSize}")
    List<User> queryAllByLimit(User user, @Param("pageable") Pageable pageable);

    @Select(" select count(1) from user")
    long count(User user);

    @Insert("INSERT INTO user (account, username, password, role, registertime,phone) VALUES (#{account} ,#{username}, #{password}, 1, NOW(), #{phone})")
    @Options(useGeneratedKeys = true, keyProperty = "userid")
    int insert(User user);


    int insertBatch(@Param("entities") List<User> entities);

    /**
     * 批量新增或按主键更新数据（MyBatis原生foreach方法）
     *
     * @param entities List<User> 实例对象列表
     * @return 影响行数
     * @throws org.springframework.jdbc.BadSqlGrammarException 入参是空List的时候会抛SQL语句错误的异常，请自行校验入参
     */
    int insertOrUpdateBatch(@Param("entities") List<User> entities);

    @Update("UPDATE user SET username = #{username}, account = #{account},password = #{password}, role = #{role} WHERE userid = #{userid}")
    int update(User user);

    @Delete("DELETE FROM user WHERE userid = #{userid}")
    int deleteById(Integer userid);
    @Select("SELECT * FROM user where account=#{account} and password = #{password}")
    User findByName(@Param("account") String account, @Param("password") String password);

    @Select("SELECT * FROM user WHERE username LIKE CONCAT('%', #{username}, '%')")
    List<User> queryByusername(String username);
}

